package com.party.mobile.web.controller.enterpriseservice;

import com.google.common.base.Strings;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.city.City;
import com.party.core.model.enterpriseservice.EpCategory;
import com.party.core.service.enterpriseservice.IEpCategoryCityService;
import com.party.core.service.enterpriseservice.IEpCategoryService;
import com.party.mobile.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 企业服务类型控制器
 * @author: Hua Qunhao
 * @create: 2019-01-08 16:45
 */
@Controller
@RequestMapping(value = "/epservice/category")
public class EnterpriseCategoryController {

    @Autowired
    private IEpCategoryService epcategoryService;
    @Autowired
    private IEpCategoryCityService epCategoryCityService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取分类列表
     *
     * @return 分类列表
     */
    //@Authorization
    @ResponseBody
    @RequestMapping(value = "/list")
    public AjaxResult list(HttpServletRequest request, String cityId) {
        if (Strings.isNullOrEmpty(cityId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不合法");
        }
        try {
            List<Map<String, Object>> datalist = new ArrayList<>();
/*            EpCategoryCity epCategoryCity = new EpCategoryCity();
            epCategoryCity.setCityId(cityId);
            List<EpCategoryCity> epCategoryCities = epCategoryCityService.list(epCategoryCity);*/

            City city = new City();
            city.setId(cityId);
/*
            if (epCategoryCities.size() == 0) {
                city = new City();
            }
*/

            List<EpCategory> epCategoryList = epcategoryService.queryEpCategoryByCity(city);

            for (EpCategory category : epCategoryList) {
                Map<String, Object> mmp = new HashMap<>();
                mmp.put("id", category.getId());
                mmp.put("categoryName", category.getName());
                datalist.add(mmp);
            }
            return AjaxResult.success(datalist);
        } catch (BusinessException be) {
            logger.error("获取分类列表异常", be);
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
    }
}
